package com.example.happiness.service.employee.Impl;

import com.example.happiness.constant.Message;
import com.example.happiness.mapper.OrderMapper;
import com.example.happiness.pojo.Order;
import com.example.happiness.pojo.employeevo.*;
import com.example.happiness.pojo.generalDTO.OrderStateDTO;
import com.example.happiness.service.employee.EmployeeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeOrderServiceImpl implements EmployeeOrderService {
    @Autowired
    OrderMapper orderMapper;

    public List<EmployOrderVO> getEmployOD(String eid) {
        return orderMapper.getEmployOD(eid);
    }

    public void updataOrderState(OrderStateDTO orderStateDTO) {
        orderMapper.updataOrderState(orderStateDTO);
    }

    public Order getOrder(String orderid) {
        return orderMapper.selectByPrimaryKey(orderid);
    }

    public List<EmployeeRecordVO> getServe(String eid) {
        return orderMapper.getServe(eid);
    }

    public EmployeeTimeLongVO getTimeLong(String eid) {
        EmployeeTimeLongVO employeeTimeLongVO = new EmployeeTimeLongVO();
        List<ERevenueVO> orders = orderMapper.getOrderTime(eid);
        if (orders != null && orders.size() > 0) {
            long i=0;
            long j=0;
            long k=0;
            for (ERevenueVO order : orders) {
                // 检查是否是今天的订单
                if (order.getOrderdate().getDayOfMonth() == LocalDateTime.now().getDayOfMonth()) {
                    i++;
                }
                // 检查是否是当月的订单
                if (order.getOrderdate().getMonth() == LocalDateTime.now().getMonth()) {
                    j++;
                }
                // 检查总完成的订单数
                if (Objects.equals(order.getState(), Message.FIVE) || Objects.equals(order.getState(), Message.NINE) ||
                        Objects.equals(order.getState(), Message.ELEVEN) || Objects.equals(order.getState(), Message.TEN)) {
                    k++;
                }
            }
            // 设置统计结果
            employeeTimeLongVO.setDay(i);
            employeeTimeLongVO.setMonth(j);
            employeeTimeLongVO.setFinish(k);
        }
        return employeeTimeLongVO;
    }

    public EmployRevenueVO getRevenue(String eid) {
        EmployRevenueVO employRevenueVO = new EmployRevenueVO();
        List<ERevenueVO> orders = orderMapper.getOrderTime(eid);
        List<EmployeeRecordVO> list = orderMapper.getServe(eid);
        if (orders != null && orders.size() > 0) {
            long k=0;
            float monthR = 0;
            float allR = 0;
            for (ERevenueVO order : orders) {
                allR = allR+(order.getPrice()*order.getProportion());
                // 检查是否是当月的订单
                if (order.getOrderdate().getMonth() == LocalDateTime.now().getMonth()) {
                    monthR = monthR+(order.getPrice()*order.getProportion());
                }
                // 检查总完成的订单数
                if (Objects.equals(order.getState(), Message.FIVE) || Objects.equals(order.getState(), Message.NINE) ||
                        Objects.equals(order.getState(), Message.ELEVEN) || Objects.equals(order.getState(), Message.TEN)) {
                    k++;
                }
            }

            employRevenueVO.setFinish(k);
            employRevenueVO.setAllRevenueVO(allR);
            employRevenueVO.setMonthRevenue(monthR);
            employRevenueVO.setServerInfo(list);
        }
        return employRevenueVO;
    }
}
